<template>
  <grid title="关于我设置">
    <el-form ref="form"
             style="text-align: left; width:1000px"
             label-width="100px"
             @submit.native.prevent="onFormSubmit"
             :model="form"
             :rules="rules">
      <el-form-item label="标题"
                    prop="about_title">
        <el-input v-model="form.about_title"
                  placeholder="请输入标题，限25个字"
                  clearable></el-input>
      </el-form-item>
      <el-form-item label="内容"
                    prop="content">
        <markdown-editor v-model="form.content">
        </markdown-editor>
      </el-form-item>
      <el-form-item>
        <el-button type="primary"
                   native-type="submit">保存</el-button>
      </el-form-item>
    </el-form>
  </grid>
</template>

<script>
import markdownEditor from '@/components/mark-down-editor'
import { mapActions, mapGetters } from 'vuex'
import { deepCopy } from '@/utils/common'

export default {
  title () {
    return '关于我设置'
  },
  data () {
    return {
      form: {
        about_title: undefined,
        content: undefined
      },
      rules: {
        about_title: [
          { required: true, message: '标题不能为空' },
          { max: 30, message: '最长25个字' }
        ],
        content: [
          { required: true, message: '内容不能为空' }
        ]
      }
    }
  },
  components: {
    markdownEditor
  },
  async asyncData ({ store, route }) {
    const { query } = route;
    return store.dispatch('setting/view', 'about')
  },
  computed: {
    ...mapGetters('setting', ['about'])
  },
  async mounted () {
    this.$nextTick(() => {
      this.form = deepCopy(this.about)
    })
  },
  methods: {
    ...mapActions('setting', ['updateAbout']),
    async onFormSubmit () {
      await this.$refs.form.validate()
      await this.updateAbout(this.form)
      this.$message.success('保存成功')
    }
  }
}
</script>
